﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Telerik.Web.UI;
using System.Web.Security;
using SalePartial.BLL;
using SalePartial.Model;
using SalePartial.Utility;

/// <summary>
/// 角色：匿名用户
/// 功能：个人用户注册
/// 布局：
/// </summary>
/// <remarks></remarks>
partial class User_login_reg : System.Web.UI.Page
{

    public User_login_reg()
    {
        Load += Page_Load;

    }
	protected void Page_Load(object sender, System.EventArgs e)
	{
		if (!IsPostBack) {
            //LoadProvinces();
            areaRelation.InitData();
            Master.FinshOneProcess(REG_PROCESS.PERSON_INFO, REG_STATUS.USED);

            UpdatePageInit();

            showPageFromSession();
		}
	}

    private bool UpdatePageInit()
    {
        RegUserInfo userinfo = (RegUserInfo)Session[ConstantSession.REG_PERPON_USER_INFO];

        btnNewPwd.Visible = false;

        if (userinfo == null || userinfo.ProviderUserKey == null)
        {
           

            return true;
        }
        else
        {
            tbUsername.Enabled = false;
            tbPassword.Text = "PWD";
            tbPassword.Enabled = false;
            tbPwdConfirm.Text = "PWD";
            tbPwdConfirm.Enabled = false;
            btnCheckUsername.Enabled = false;

            securityQuestion.Enabled = false;
            securityQuestion.SecurityAnswer = "********";
        }




        return true;
    }

	protected void btn_chk_Click(object sender, EventArgs e)
	{
		if (string.IsNullOrEmpty(tbUsername.Text)) {

            RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "aa", "alert('请输入用户名！')", true);
			return;
		}
		if (Membership.GetUser(tbUsername.Text) == null) {
            RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "aa", "alert('用户名可以使用！')", true);
			return;
		} else {
			tbUsername.Text = String.Empty;
            RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "aa", "alert('用户已存在！')", true);

			return;
		}
	}

    protected void ibtSupply_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        RadCaptchaReg.Validate();
        if (!RadCaptchaReg.IsValid)
        {
            return;
        }

        saveToSession();

        Page.Response.Redirect("~/Login/regSupplies.aspx", true);
    }

    protected void ibtPurchase_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        RadCaptchaReg.Validate();
        if (!RadCaptchaReg.IsValid)
        {
            return;
        }

        saveToSession();

        Page.Response.Redirect("~/Login/regPurchases.aspx", true);
    }

	protected void ibtSubmit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
	{
        RadCaptchaReg.Validate();
		if ( !RadCaptchaReg.IsValid) {
			return;
		}

        saveToSession();

        Page.Response.Redirect("~/Login/regConfirm.aspx", true);

	}

    private bool showPageFromSession()
    {
        XPUserProfileInfo userProfileInfo = (XPUserProfileInfo)Session[ConstantSession.REG_PERPON_USER_PROFILE];
        RegUserInfo userinfo = (RegUserInfo)Session[ConstantSession.REG_PERPON_USER_INFO];

        if (userProfileInfo == null || userinfo == null)
        {
            return true;
        }
        tbUsername.Text = userinfo.Username;
        //tbUsername.Enabled = false;

        tbPassword.Text = userinfo.Password;
        tbPwdConfirm.Text = userinfo.Password;

        securityQuestion.SelectedSecurityQuestion = userinfo.PasswordQuestion;
        securityQuestion.SecurityAnswer = userinfo.PasswordAnswer;

        rtbPosition.Text = userProfileInfo.JobTitle;
        tbLastname.Text = userProfileInfo.FamilyName;
        tbFirstname.Text = userProfileInfo.FirstName;
        tbCellphone.Text = userProfileInfo.CellPhone;
        tbPhone.Text = userProfileInfo.OfficePhone;
        areaRelation.SelectedCountry = userProfileInfo.Country;
        areaRelation.SelectedProvince = userProfileInfo.Province;
        areaRelation.SelectedCity = userProfileInfo.City;
        tbZipcode.Text = userProfileInfo.PostCode;

        if (userProfileInfo.NotificationCollection != string.Empty)
        {
            string[] tmp = userProfileInfo.NotificationCollection.Split(new string[] { Constant.STR_SPLIT_LISTITEM }, StringSplitOptions.RemoveEmptyEntries);
            foreach (RadListBoxItem item in rlbNotification.Items)
            {

                foreach (string text in tmp)
                {
                    if(item.Text == text)
                    {
                        item.Checked = true;
                        break;
                    }
                }
            }
            
        }

        cadListCommbox.SetSelectedString = userProfileInfo.Software;

        return true;
    }

    private bool saveToSession()
    {
        string securityQ = securityQuestion.SelectedSecurityQuestion;

        //if (rcbSecurityQuestion.SelectedIndex > 0) {
        //    securityQ = rcbSecurityQuestion.SelectedValue;

        //} else {
        //    securityQ = rcbSecurityQuestion.Text;
        //}
        XPUserProfileInfo newUEP = null;
        RegUserInfo userinfo = null;
        if (Session[ConstantSession.REG_PERPON_USER_PROFILE] != null)
        {
            newUEP = (XPUserProfileInfo)Session[ConstantSession.REG_PERPON_USER_PROFILE];
        }
        else
        {
            newUEP = new XPUserProfileInfo();
        }

        if (Session[ConstantSession.REG_PERPON_USER_INFO] != null)
        {
            userinfo = (RegUserInfo)Session[ConstantSession.REG_PERPON_USER_INFO];
        }
        else
        {
            userinfo = new RegUserInfo();
        }

        var _with1 = newUEP;

        _with1.JobTitle = rtbPosition.Text.Trim();

        _with1.FamilyName = tbLastname.Text.Trim();
        _with1.FirstName = tbFirstname.Text.Trim();
        _with1.OfficePhone = tbPhone.Text;
        _with1.CellPhone = tbCellphone.Text;

        _with1.Country = areaRelation.SelectedCountry;
        _with1.Province = areaRelation.SelectedProvince;
        _with1.City = areaRelation.SelectedCity;

        _with1.PostCode = tbZipcode.Text;

        foreach (RadListBoxItem item in rlbNotification.CheckedItems)
        {
            _with1.NotificationCollection += item.Text + Constant.STR_SPLIT_LISTITEM;
        }

        foreach (RadListBoxItem item in cadListCommbox.SelectedCadList)
        {
            _with1.Software += item.Text + Constant.STR_SPLIT_LISTITEM;
        }

        Session[ConstantSession.REG_PERPON_USER_PROFILE] = _with1;

        userinfo.Username = tbUsername.Text.Trim();
        userinfo.Password = tbPassword.Text.Trim();
        userinfo.Email = tbUsername.Text.Trim();
        userinfo.PasswordQuestion = securityQ;
        userinfo.PasswordAnswer = securityQuestion.SecurityAnswer;
        userinfo.IsApproved = true;

        Session[ConstantSession.REG_PERPON_USER_INFO] = userinfo;

        Session[ConstantSession.PRE_PAGE_ID] = Request.FilePath;
        return true;
    }

    protected void btnCheckUsername_Click(object sender, System.EventArgs e)
    {
        if (string.IsNullOrEmpty(tbUsername.Text))
        {
            lblInvalidUsername.Visible = true;
            lblInvalidUsername.Text = "·请输入邮箱地址";
            return;
        }

        if (Membership.GetUser(tbUsername.Text) == null)
        {
            lblInvalidUsername.Visible = true;
            lblInvalidUsername.Text = "·邮箱地址可以使用";
            return;

        }
        else
        {
            tbUsername.Text = String.Empty;
            lblInvalidUsername.Visible = true;
            lblInvalidUsername.Text = "·邮箱地址已被使用";
            return;
        }


    }

    protected void btnNewPwd_Click(object sender, EventArgs e)
    {
        if (btnNewPwd.Text == "保留原密码")
        {
            btnNewPwd.Text = "重设密码";
            tbPassword.Enabled = true;
            tbPassword.Text = "";
            tbPwdConfirm.Enabled = true;
            tbPwdConfirm.Text = "";
        }
        else
        {
            btnNewPwd.Text = "保留原密码";
            tbPassword.Enabled = false;
            tbPassword.Text = "PWD";
            tbPwdConfirm.Enabled = false;
            tbPwdConfirm.Text = "PWD";
        }
    }
}
